import json
import re


def gen_json(filename, result_dict):
    # 持久化到 JSON 文件
    with open(filename, "w") as f:
        json.dump(result_dict, f, indent=4)

def load_json(filename):
    # 从 JSON 文件中读取字典
    with open(filename, "r") as f:
        return json.load(f)


def gen_upc(data):
    print(json.dumps(data))

def gen_data_definitions(data, dest_path):
    res_dict ={}
    for row_idx in range(len(data)):
        if data[row_idx][0] is None and data[row_idx][1] is not None:
            field_name  = re.sub(r'\[[^\]]*\]', '', data[row_idx][1])
            tmp = {}
            definition_and_use = data[row_idx][3]
            accepted_values = data[row_idx][4]
            example = data[row_idx][5]
            tmp["Accepted_Values"] = accepted_values
            tmp["Example"] = example
            res_dict.update({field_name: tmp})
    # print(json.dumps(res_dict))
    gen_json(dest_path, res_dict)


def required_field(data, dest_path):
    res = []
    for row_idx in range(len(data)):
        if data[row_idx][0] is None and data[row_idx][1] is not None and data[row_idx][5] is not None and data[row_idx][5] in ["Required","Conditionally Required"]:
            field_name = data[row_idx][1]
            field_name  = re.sub(r'\[[^\]]*\]', '', field_name)
            res.append(field_name)
    # print(json.dumps(res))
    gen_json(dest_path, res)


def field_map(data, dest_path):
    result_map = {}
    # print(data)
    count_map = {}
    for col_index in range(len(data[3])):
        human_name = data[3][col_index]
        field_name = re.sub(r'\[[^\]]*\]', '', data[4][col_index])
        count = count_map.get(human_name, 0)
        origin_name = human_name
        if count:
            human_name = human_name[:-1] + str(count)
            result_map.update({human_name:field_name})
        else:
            result_map.update({human_name:field_name})
        count_map[origin_name] = count + 1
    # print(json.dumps(result_map))
    gen_json(dest_path, result_map)
    return result_map

def template_filed(data, dest_path):
    res = []
    for col_index in range(len(data[4])):
        field_name = re.sub(r'\[[^\]]*\]', '', data[4][col_index])
        res.append(field_name)
    # print(json.dumps(res))
    gen_json(dest_path, res)


# 提取valid values规则,传入整个二维列表, 字段大小写映射, 生成json地址
def values(data, dest_path, field_map, drop_list_path):
    res = {}
    for row in range(len(data)):
        if data[row][0] is None:
            row_data = data[row]
            row_data = [x for x in row_data if x is not None]
            field_name = row_data[0].split(" - [ ")[0]
            field_name = field_map.get(field_name)
            field_list = row_data[1:]
            # 遇到重复的了
            if res.get(field_name) is not None:
                drop_dict = load_json(drop_list_path)
                drop_list = drop_dict.get(field_name, [])
                pre_values_list = res.get(field_name)
                if len(drop_list) > 0:
                    if sorted(drop_list) == sorted(pre_values_list):
                        field_list = pre_values_list
                elif len(pre_values_list) > len(field_list):
                    field_list = pre_values_list
            res.update({field_name: field_list})
    # print(json.dumps(res))
    gen_json(dest_path, res)

# 提取drop规则,传入整个二维列表,生成的json地址
def drop(data, dest_path):
    dict_res = {}
    # 获取列数
    num_cols = len(data[2])
    # 按列循环操作
    for col in range(2,num_cols,1):
        column_data = [row[col] for row in data]  # 提取当前列的数据
        #column_data = [x for x in column_data if x]
        field_name = re.sub(r'\[[^\]]*\]', '', column_data[2])
        field_list = column_data[3:]
        field_list = [x for x in field_list if x]
        dict_res.update({field_name: field_list})
    # print(json.dumps(dict_res))
    gen_json(dest_path, dict_res)


def get_product_type(data):
    return data[3][-2]




if __name__ == '__main__':
    "".replace("#", "")
    pass
